package com.d3works.apps.stock.service.app;

import com.d3works.apps.base.db.AppStock;
import com.d3works.apps.base.db.Company;
import com.d3works.apps.base.db.repo.AppStockRepository;
import com.d3works.apps.base.db.repo.CompanyRepository;
import com.d3works.apps.stock.db.StockConfig;
import com.d3works.apps.stock.db.repo.StockConfigRepository;
import com.google.inject.Inject;
import com.google.inject.persist.Transactional;
import java.util.List;

public class AppStockServiceImpl implements AppStockService {

  @Inject private CompanyRepository companyRepo;

  @Inject private StockConfigRepository stockConfigRepo;

  @Inject private AppStockRepository appStockRepository;

  @Override
  @Transactional
  public void generateStockConfigurations() {

    List<Company> companies = companyRepo.all().filter("self.stockConfig is null").fetch();

    for (Company company : companies) {
      StockConfig stockConfig = new StockConfig();
      stockConfig.setCompany(company);
      stockConfigRepo.save(stockConfig);
    }
  }

  @Override
  public AppStock getAppStock() {
    return appStockRepository.all().fetchOne();
  }
}
